//
//  RejectViewController.m
//  KIASupervisory
//
//  Created by tan_xin on 14-8-19.
//  Copyright (c) 2014年 MyCompany. All rights reserved.
//

#import "RejectViewController.h"

@interface RejectViewController ()

@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet UITextView *rejectTv;

- (IBAction)dismissRejectViewAction:(id)sender;
- (IBAction)submitRejectAction:(id)sender;


@end

@implementation RejectViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self addKeyboardNotificationForTarget:self];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2];
    
    [self.rejectTv becomeFirstResponder];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)dismissRejectViewAction:(id)sender {
    [self.view removeFromSuperview];
    [self removeFromParentViewController];
}

- (IBAction)submitRejectAction:(id)sender {
    if ([self.rejectTv hasText]) {
        if (_delegate && [_delegate respondsToSelector:@selector(reject:willRejctWithReason:)]) {
            [self.delegate reject:self willRejctWithReason:self.rejectTv.text];
        }
        [self dismissRejectViewAction:nil];
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                        message:@"驳回理由不能为空，请填写驳回理由之后再提交"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles: nil];
        [alert show];
    }
}

#pragma mark -
#pragma mark keyboard notification
//监听键盘出现、消失和改变大小的事件。当切换至中文键盘，键盘高度改变时相应的改变键盘上方的控件
- (void)keyboardWillShow:(NSNotification*)notification {
    NSDictionary *userInfo = [notification userInfo];
    
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    CGRect keyboardFrame = [self.view convertRect:keyboardRect fromView:[[UIApplication sharedApplication] keyWindow]];
    CGFloat keyboardHeight = keyboardFrame.size.height;
    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval animationDuration;
    [animationDurationValue getValue:&animationDuration];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:animationDuration];
    
    float originY = CGRectGetHeight(self.view.bounds) - keyboardHeight - CGRectGetHeight(self.contentView.bounds);
    self.contentView.frame = CGRectMake(5, originY, CGRectGetWidth(self.contentView.bounds),
                                        CGRectGetHeight(self.contentView.bounds));
    
    
    [UIView commitAnimations];
}

- (void)keyboardWillHide:(NSNotification*)notification {
    NSDictionary *userInfo = [notification userInfo];
    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval animationDuration;
    [animationDurationValue getValue:&animationDuration];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:animationDuration];
    
    //    self.topView.frame = CGRectMake(0, 0, CGRectGetWidth(self.topView.bounds), CGRectGetHeight(self.topView.bounds));
    
    [UIView commitAnimations];
}
@end
